package com.cuiyun97.controller.admin;

import com.cuiyun97.domain.dto.AdminDTO;
import com.cuiyun97.domain.dto.AdminPageQueryDTO;
import com.cuiyun97.domain.po.Admin;
import com.cuiyun97.domain.vo.AdminVO;
import com.cuiyun97.properties.JwtProperties;
import com.cuiyun97.result.PageResult;
import com.cuiyun97.result.Result;
import com.cuiyun97.service.AdminService;
import com.cuiyun97.service.SetJwtService;
import com.cuiyun97.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "管理员接口")
@RequestMapping("/admin/admin")
public class AdminController {

    @Resource
    private JwtProperties jwtProperties;

    @Resource
    private AdminService adminService;

    // jwt设置
    @Resource
    private SetJwtService jwtService;

    /**
     * 管理员登录
     *
     * @param adminDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    public Result<AdminVO> login(@RequestBody AdminDTO adminDTO) {
        log.info("管理员登录: {}", adminDTO);
        Admin admin = adminService.login(adminDTO);

        // 生成jwt令牌
        Long admin_ttl = jwtService.getLastOne().getAdminTtl();

        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", admin.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                // jwtProperties.getAdminTtl() * 1000,
                admin_ttl * 1000,
                claims
        );

        // 设置token, 并返回对象
        AdminVO adminVO = AdminVO.builder()
                .id(admin.getId())
                .username(admin.getUsername())
                .name(admin.getName())
                .token(token)
                .build();

        return Result.success(adminVO);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result logout() {
        log.info("退出登录");
        return Result.success();
    }

    /**
     * 添加管理员
     *
     * @param adminDTO
     * @return
     */
    @PostMapping()
    @ApiOperation("新增管理员")
    public Result save(@RequestBody AdminDTO adminDTO) {
        log.info("新增管理员: {}", adminDTO);
        adminService.save(adminDTO);
        return Result.success();
    }

    /**
     * 根据ID查询管理员
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询管理员")
    public Result<AdminVO> getById(@PathVariable Integer id) {
        log.info("根据ID查询管理员: {}", id);
        AdminVO adminVO = adminService.getById(id);
        return Result.success(adminVO);
    }

    /**
     * 修改管理员
     * @param adminDTO
     * @return
     */
    @PutMapping()
    @ApiOperation("修改管理员")
    public Result update(@RequestBody AdminDTO adminDTO) {
        adminService.update(adminDTO);
        return Result.success();
    }

    /**
     * 状态修改
     *
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用管理员")
    public Result status(@PathVariable Integer status, Integer id) {
        log.info("启用或禁用管理员: {}, {}", status, id);
        adminService.status(status, id);
        return Result.success();
    }

    /**
     * 删除操作
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("批量删除")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除管理员: {}", ids);
        adminService.delete(ids);
        return Result.success();
    }


    /**
     * 根据条件分页查询管理员
     *
     * @param adminPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("根据条件分页查询")
    public Result page(AdminPageQueryDTO adminPageQueryDTO) {
        log.info("根据条件分页查询: {}", adminPageQueryDTO);
        PageResult pageResult = adminService.pageQuery(adminPageQueryDTO);
        return Result.success(pageResult);
    }

}
